package mpbe.geometria;

/*
 * Este codigo es software libre y se encuentra licenciado 
 * bajo los términos de la Licencia Pública General de GNU versión 3 
 * según se encuentra publicada por la Free Software Foundation.
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Autor: Martin Pablo Bellanca <martin.bellanca@gmail.com>
 * web: http://mpbe.blogspot.com/
 */

/**
 * @author Martin Pablo Bellanca <martin.bellanca@gmail.com>
 *
 */
public class Recta {
	private Punto pi;
	private Punto pf;
	
	
	//*****************************************************
	// Constructores
	//*****************************************************
	/**
	 * 
	 */
	public Recta() {
		pi = new Punto();
		pf = new Punto();
	}
	public Recta(Punto pi, Punto pf) {
		this.pi = pi;
		this.pf = pf;
	}
	
	
	//*****************************************************
	// geters and setters
	//*****************************************************
	/**
	 * @return the pi
	 */
	public Punto getPi() {
		return pi;
	}
	/**
	 * @param pi the pi to set
	 */
	public void setPi(Punto pi) {
		this.pi = pi;
	}
	/**
	 * @return the pf
	 */
	public Punto getPf() {
		return pf;
	}
	/**
	 * @param pf the pf to set
	 */
	public void setPf(Punto pf) {
		this.pf = pf;
	}
	
	public double getDeltaX(){return (pf.getX()-pi.getX());}
	public double getDeltaY(){return (pf.getY()-pi.getY());}
	public double getRadioVector(){return (Math.sqrt(Math.pow(getDeltaX(), 2)+Math.pow(getDeltaY(), 2)));}
	public Vector getVector(){return new Vector (getDeltaX(), getDeltaY());}
	public Vector getNormal(){return new Vector (getDeltaY(), -1*getDeltaX());}
	public Vector getDirector(){return new Vector (getDeltaX()/getRadioVector(), getDeltaY()/getRadioVector());}
	public Vector getDirectorNormal(){return new Vector (getDeltaY()/getRadioVector(), -1*getDeltaX()/getRadioVector());}
	public double getY(double X) {
		double dx = getDeltaX();
		double dy = getDeltaY();
		if (dx!=0) 
			return (X-pi.X)*dy/dx+pi.Y;
		else if (X == pi.X) 
			return 0;
		else return Double.POSITIVE_INFINITY;
	}
	
	public void copyinto(Recta recta) {
		recta.pi = pi;
		recta.pf = pf;
	}
	

	//*****************************************************
	// Fundiones de la Recta
	//*****************************************************
	public Recta offset(double dist) {
		Vector r = getDirectorNormal().escalar(dist);
		Punto pio = pi.mover(r.getX(), r.getY());
		Punto pfo = pf.mover(r.getX(), r.getY());
		return new Recta(pio, pfo);
	}
	public Vector distancia(Punto punto) {
		Vector vp = getNormal();
		Recta rp = new Recta(punto, punto.mover(vp.getX(), vp.getY()));
		Punto pi = interseccion(rp);
		return new Vector(pi.getX()-punto.getX(), pi.getY()-punto.getY());
	}
	public Punto interseccion(Recta recta2) {
		double alf1 = getDeltaY()/getDeltaX();
		double alf2 = recta2.getDeltaY()/recta2.getDeltaX();		
		double px;
		double py;

		if (getDeltaX() == 0) { //Recta 1 vertical
			px = pi.X;
			py = (px-recta2.pi.getX())*alf2+recta2.pi.getY();
		} else if (recta2.getDeltaX() == 0) { //Recta 2 vertical
			px = recta2.pi.X;
			py = (px-pi.getX())*alf1+pi.getY();
		} else if (alf1!=alf2) {// verifica paralelismo entre rectas
			Punto p2 = recta2.getPi();
			px = (pi.getY()-p2.getY()-alf1*pi.getX()+alf2*p2.getX())/(alf2-alf1);
			py = (px-pi.getX())*alf1+pi.getY();
		} else return null;
		return new Punto(px, py);
	}
	
	public boolean isParalela(Recta recta2) {
		double dx = getDeltaX();
		double dy = getDeltaY();
		double d2x = recta2.getDeltaX();
		double d2y = recta2.getDeltaY();
		
		if ((dx==0 && d2x==0) || (dy==0 && d2y==0))
			return true;
		else if ((dx/d2x)==(dy/d2y))
			return true;
		else 
			return false;
	}
	public boolean isColineal(Recta recta2) {
		if ((this.getY(pi.X) == recta2.getY(pi.X)) && this.isParalela(recta2)) 		
			return true;
		else 
			return false;
	}

}
